package com.gitee.smalllog.server;

import com.gitee.smalllog.core.client.AbstractClient;
import com.gitee.smalllog.core.constant.InitConfig;
import com.gitee.smalllog.core.constant.LogMessageConstant;
import com.gitee.smalllog.core.utils.IndexUtil;
import com.gitee.smalllog.server.client.DataBaseClient;
import com.gitee.smalllog.server.collect.SmallLogCollect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(100)
public class CollectStartBean implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(CollectStartBean.class);

    @Autowired
    private DataBaseClient dataBaseClient;

    @Autowired(required = false)
    private AbstractClient redisClient;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Value("${small.redis.compressor:false}")
    private Boolean compressor;


    /**
     * 初始化日志监听收集服务
     */
    private void serverStart() {
        SmallLogCollect redisLogCollect = new SmallLogCollect(dataBaseClient, redisClient,
                applicationEventPublisher, compressor, redisClient);
        redisLogCollect.redisStart();
    }


    @Override
    public void afterPropertiesSet() {
        try {
            //todo
            /*if (InitConfig.ES_INDEX_MODEL.equals("hour") && !InitConfig.START_MODEL.equals(InitConfig.LITE_MODE_NAME)) {
                abstractServerClient.addShards(InitConfig.maxShards);
                logger.info("set es max_shards_per_node of :" + InitConfig.maxShards);
            }
            if (!InitConfig.START_MODEL.equals(InitConfig.LITE_MODE_NAME)) {
                autoCreatIndice();
            }*/
            serverStart();
        } catch (Exception e) {
            logger.error("plumelog server starting failed!", e);
        }
    }

    /*private void autoCreatIndice() {
        long epochMillis = System.currentTimeMillis();
        String runLogIndex = IndexUtil.getRunLogIndex(epochMillis);
        String traceLogIndex = IndexUtil.getTraceLogIndex(epochMillis);
        if ("day".equals(InitConfig.ES_INDEX_MODEL)) {
            creatIndiceLog(runLogIndex);
            creatIndiceTrace(traceLogIndex);
        } else {
            for (int a = 0; a < 24; a++) {
                String hour = String.format("%02d", a);
                creatIndiceLog(runLogIndex + hour);
                creatIndiceTrace(traceLogIndex + hour);
            }
        }
    }

    private void creatIndiceLog(String index) {
        if (!abstractServerClient.existIndice(index)) {
            abstractServerClient.creatIndice(index, LogMessageConstant.ES_TYPE);
        }
    }

    private void creatIndiceTrace(String index) {
        if (!abstractServerClient.existIndice(index)) {
            abstractServerClient.creatIndiceTrace(index, LogMessageConstant.ES_TYPE);
        }
    }*/
}
